﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Xml.Linq;
using WSManager.Model;

namespace WSManager.Wpf
{
    public class ArticleViewModel: ViewModelBase<Article>
    {
        public ArticleViewModel(Article article, Web parent)
        {
            this.Article = article;
            this._parent = parent;
        }

        public ArticleViewModel(Web parent) : this(new Article(), parent) { }

        Web _parent;
        Article _article;
        public Article Article
        {
            get
            {
                return _article;
            }
            set
            {
                _article = value;
                RaisePropertyChanged("Name");
                RaisePropertyChanged("Content");
                RaisePropertyChanged("Description");
            }
        }

        public bool Checked { get; set; }

        public string Name
        {
            get { return Article.Name; }
            set
            {
                Article.Name = value;
                RaisePropertyChanged("Name");
            }
        }

        public string Description
        {
            get
            {
                return Article.Description;
            }
            set
            {
                Article.Description = value;
                RaisePropertyChanged("Description");
            }
        }

        public XElement Content
        {
            get
            {
                return Article.Content;
            }
        }

        #region commands

        public ICommand EditArticle { get { return new RelayCommand(Edit); } }

        private void Edit()
        {
            ArticleEditor.ArticleEditorManager.BeginEditArticle(this.Article, a => { 
                this.Article = a; }, _parent);
        }

        #endregion

        //public event EventHandler EditingComplete;

        //public static void NewArticle()
        //{
        //    var article = new ArticleViewModel(new Article());
        //    article.Edit();
        //    article.EditingComplete += (sender, args) =>
        //    {
        //        owner.AddArticle(article);
        //    };
        //}

        //internal void EditComplete()
        //{
        //    EditingComplete(this, new EventArgs());
        //}
    }
}
